package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.entity.IntegralGoods;
import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.EnterpriseWeChat.mappers.EWIntegralGoodsMapper;
import com.burgeon.backend.EnterpriseWeChat.rest.integral.*;
import com.burgeon.backend.EnterpriseWeChat.service.EWIntegralGoodsService;
import com.burgeon.backend.EnterpriseWeChat.service.EWOrderService;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Service
public class EWIntegralGoodsServiceImpl implements EWIntegralGoodsService {
    private final static Log logger = LogFactory.getLog(EWOrderService.class);
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private EWIntegralGoodsMapper ewIntegralGoodsMapper;
    private Info GetInfoByToken(String token) {
        return accountMapper.GetTokenData(token);
    }

    @Override
    public List<IntegralGoods> GetIntegralGoods(String token, int page) {
        Info info = GetInfoByToken(token);
        page=(page-1)*10;
        //积分商品
        List<IntegralGoods> integralGoods = ewIntegralGoodsMapper.GetIntegralGoods(token,page);
        GetIntegralGoodsByCode getIntegralGoodsByCode = new GetIntegralGoodsByCode(info.getUrl(), info.getAccount(), info.getPassword());

        for (IntegralGoods goods:integralGoods) {
            try {
                //查询商品的库存和标准价
                HashMap map = getIntegralGoodsByCode.getData(goods.getCode());
                if(null != map){
//                    goods.setPrice(map.get("price").toString());
                    goods.setInventory(map.get("inventory").toString());
                }else {
//                    goods.setPrice("0");
                    goods.setInventory("0");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return integralGoods;
    }

    @Override
    public List<IntegralGoods> FindIntegralGoodsByCode(String token, String content, int page) {
        Info info = GetInfoByToken(token);
        page=(page-1)*10;
        List<IntegralGoods>  integralGoods = ewIntegralGoodsMapper.FindIntegralGoodsByCode(token,content,page);
        GetIntegralGoodsByCode getGoodsByCode = new GetIntegralGoodsByCode(info.getUrl(), info.getAccount(),
                info.getPassword());
        for (IntegralGoods goods:integralGoods) {
            try {
                //查询商品的库存和标准价
                HashMap map = getGoodsByCode.getData(goods.getCode());
                if(null != map){
//                    goods.setPrice(map.get("price").toString());
                    goods.setInventory(map.get("inventory").toString());
                }else {
//                    goods.setPrice("0");
                    goods.setInventory("0");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return integralGoods;
    }

    /**
     * 积分活动邀约页面的商品信息
     * @param id
     * @param token
     * @return
     */
    @Override
    public IntegralGoods GetIntegralGoodsById(String id, String token) {
        Info info = GetInfoByToken(token);
        IntegralGoods goods = ewIntegralGoodsMapper.GetIntegralGoodsById(id,token);
        logger.info(goods);
        GetIntegralGoodsByCode getGoodsByCode = new GetIntegralGoodsByCode(info.getUrl(), info.getAccount(),
                info.getPassword());
        try {
            //查询商品的库存和标准价
            HashMap map = getGoodsByCode.getData(goods.getCode());
//            goods.setPrice(map.get("price").toString());
            if(null != map){
//                    goods.setPrice(map.get("price").toString());
                goods.setInventory(map.get("inventory").toString());
            }else {
//                    goods.setPrice("0");
                goods.setInventory("0");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return goods;
    }

    /**
     * 全部的积分活动邀约列表
     * @param empId
     * @param integral 商品积分
     * @param token
     * @param page
     * @param code 款号
     * @return
     */
    @Override
    public List<Vip> GetIntegralVipByEmp(String empId, String integral, String token, int page, String code) {
        Info info = GetInfoByToken(token);
        GetIntegralVipByEmp getIntegralVipByEmp = new GetIntegralVipByEmp(info.getUrl(), info.getAccount(),
                info.getPassword());
        GetIsBJ getIsBJ = new GetIsBJ(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            List<Vip> vips = getIntegralVipByEmp.getData(empId,page,integral);
            if(vips.size()>0){
                for (Vip vip:vips) {
                    HashMap map = getIsBJ.getData(code,vip.getId());
                    if(null != map){
                        vip.setBJ("已回访");
                        vip.setFeedBack(map.get("feedBack").toString());
                        vip.setTarGetDate(map.get("tarGetDate").toString());
                    }else {
                        vip.setBJ("未回访");
                    }
                }
                return vips;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 已回访 未回访
     * @param empId
     * @param integral
     * @param token
     * @param page
     * @param code
     * @param type
     * @return
     */
    @Override
    public List<Vip> GetIntegralVipByType(String empId, String integral, String token, int page, String code, String type) {
        Info info = GetInfoByToken(token);
        GetIntegralVipByEmp getIntegralVipByEmp = new GetIntegralVipByEmp(info.getUrl(), info.getAccount(),
                info.getPassword());
        GetIsBJ getIsBJ = new GetIsBJ(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            List<Vip> vips = getIntegralVipByEmp.getData(empId,page,integral);
            Iterator<Vip> vipIterator=vips.iterator();
//            while(vipIterator.hasNext()){
//                Vip a=vipIterator.next();
//
//            }
            if(vips.size()>0){
                //type=1是已回访，2是为回访
                if("1".equals(type)){
                    while(vipIterator.hasNext()){
                        Vip vip=vipIterator.next();
                        HashMap map = getIsBJ.getData(code,vip.getId());
                        if(null != map){
                            vip.setBJ("已回访");
                            vip.setFeedBack(map.get("feedBack").toString());
                            vip.setTarGetDate(map.get("tarGetDate").toString());
                            vip.setRecentDate(map.get("BILLDATE").toString());
                        }else {
//                            vip.setBJ("未回访");
                            vipIterator.remove();
                        }
                    }
                }else if("2".equals(type)){
                    while(vipIterator.hasNext()){
                        Vip vip=vipIterator.next();
                        HashMap map = getIsBJ.getData(code,vip.getId());
                        if(null != map){
                            vipIterator.remove();
//                            vip.setBJ("已回访");
//                            vip.setFeedBack(map.get("feedBack").toString());
//                            vip.setTarGetDate(map.get("tarGetDate").toString());
                        }else {
                            vip.setBJ("未回访");

                        }
                    }
                }
                return vips;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public HashMap GetCounts(String empId, String integral, String token,String code) {
        Info info = GetInfoByToken(token);
        GetTotCount getTotCount = new GetTotCount(info.getUrl(), info.getAccount(), info.getPassword());
        GetIsBJCounts getIsBJ = new GetIsBJCounts(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            Integer totCount = getTotCount.getData(empId,integral);
//            ArrayList<String> doneIds= new ArrayList<>();
//            ArrayList<String> waitIds= new ArrayList<>();
//            for (String id :ids) {
//                HashMap map = getIsBJ.getData(code,id);
//                //doneCount
//                if(null != map){
//                    //已邀约
//                    doneIds.add(id);
//                }else {
//                    //未邀约
////                    waitIds.add(id);
//                }
//            }
            HashMap map = new HashMap();
            map.put("count",totCount);//总
            map.put("doneCount",getIsBJ.getData(code,empId));//已邀约
//            map.put("waitCount",waitIds.size());//未邀约
            return map;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
